#include <QFileDialog>
#include <QUrl>
#include <QSettings>
#include "testwidget.h"
#include "ui_testwidget.h"
#include "video/renderthread.h"


TestWidget::TestWidget(QWidget *parent) :
    QWidget(parent),
    m_setting("config.ini",QSettings::IniFormat),
    ui(new Ui::TestWidget)
{
    ui->setupUi(this);

    ui->comboBox_url->setCurrentText(m_setting.value("last_file").toString());

    this->m_videoCtls.push_back(ui->frame_1);
    this->m_videoCtls.push_back(ui->frame_2);
    this->m_videoCtls.push_back(ui->frame_3);
    this->m_videoCtls.push_back(ui->frame_4);
    this->m_videoCtls.push_back(ui->frame_5);
    this->m_videoCtls.push_back(ui->frame_6);
    this->m_videoCtls.push_back(ui->frame_7);
    this->m_videoCtls.push_back(ui->frame_8);
    this->m_videoCtls.push_back(ui->frame_9);
    this->m_videoCtls.push_back(ui->frame_10);
    this->m_videoCtls.push_back(ui->frame_11);
    this->m_videoCtls.push_back(ui->frame_12);
    this->m_videoCtls.push_back(ui->frame_13);
    this->m_videoCtls.push_back(ui->frame_14);
    this->m_videoCtls.push_back(ui->frame_15);
    this->m_videoCtls.push_back(ui->frame_16);
}

TestWidget::~TestWidget()
{
    delete ui;
}

void TestWidget::on_pushButton_file_clicked()
{
    QString url = QFileDialog::getOpenFileName(this);
    ui->comboBox_url->setCurrentText(url);

    m_setting.setValue("last_file",url);
}

void TestWidget::on_pushButton_start_clicked()
{
    QString text=ui->comboBox_frame->currentText();
    MediaInfo media;
    media.url=ui->comboBox_url->currentText();

    if(text=="全部"){
        for(int i=0;i<m_videoCtls.size();i++){
            m_videoCtls[i]->player()->play(media);
        }
    }else{
        int index = text.toInt()-1;
        m_videoCtls[index]->player()->play(media);
    }
}

void TestWidget::on_pushButton_pause_clicked()
{
    QString text=ui->comboBox_frame->currentText();
    MediaInfo media;
    media.url=ui->comboBox_url->currentText();

    if(text=="全部"){
        for(int i=0;i<m_videoCtls.size();i++){
            m_videoCtls[i]->player()->pause();
        }
    }else{
        int index = text.toInt()-1;
        m_videoCtls[index]->player()->pause();
    }
}

void TestWidget::on_pushButton_resume_clicked()
{
    QString text=ui->comboBox_frame->currentText();
    MediaInfo media;
    media.url=ui->comboBox_url->currentText();

    if(text=="全部"){
        for(int i=0;i<m_videoCtls.size();i++){
            m_videoCtls[i]->player()->resume();
        }
    }else{
        int index = text.toInt()-1;
        m_videoCtls[index]->player()->resume();
    }
}

void TestWidget::on_pushButton_stop_clicked()
{
    QString text=ui->comboBox_frame->currentText();
    MediaInfo media;
    media.url=ui->comboBox_url->currentText();

    if(text=="全部"){
        for(int i=0;i<m_videoCtls.size();i++){
            m_videoCtls[i]->player()->stop();
        }
    }else{
        int index = text.toInt()-1;
        m_videoCtls[index]->player()->stop();
    }
}

void TestWidget::on_pushButton_seek_clicked()
{
    QString text=ui->comboBox_frame->currentText();
    qint64 pos = ui->doubleSpinBox_pos->value()*1000;

    if(text=="全部"){
        for(int i=0;i<m_videoCtls.size();i++){
            m_videoCtls[i]->player()->seek(pos);
        }
    }else{
        int index = text.toInt()-1;
        m_videoCtls[index]->player()->seek(pos);
    }
}
